package com.base.pig.controller;


import com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.util.internal.StringUtil;
import com.base.pig.entity.ScTypeSensor;
import com.base.pig.service.ScSensorInfoService;
import com.base.pig.service.ScTypeSensorService;
import com.base.pig.service.SmregisterService;
import com.base.pig.util.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author mag
 * @since 2022-05-09
 */
@RestController
@RequestMapping("/type-sensor")
@Api(value = "type-sensor", tags = "类型-传感器关系绑定")
public class ScTypeSensorController {

    @Autowired
    public ScTypeSensorService scTypeSensorService;

    @Autowired
    public ScSensorInfoService  scSensorInfoService;

    @Autowired
    public SmregisterService smregisterService;



    @ApiOperation(value = "根据deviceType,查询已绑定传感器信息")
    @GetMapping("/getSensorByType/{deviceType}")
    public R getSensorByType(@PathVariable("deviceType") String deviceType){

        //校验
        if(deviceType==null    ||   "".equals(deviceType)){
            return R.failed(null,"设备类型不能为空");
        }

        List<Map<String, Object>> bingList=scSensorInfoService.querySensorByDeviceType(deviceType);

        return R.ok(bingList,"查询成功");
    }

    @ApiOperation(value = "查询全部传感器编码")
    @GetMapping("/getAllSensor}")
    public R getAllSensor(){

        List<Map<String, Object>> allList = scSensorInfoService.queryAllSensorCode();

        return R.ok(allList,"查询成功");
    }


    @ApiOperation(value = "根据deviceid,查询传感器")
    @GetMapping("/getSensorByDevice/{deviceid}")
    public R getSensorByDevice(@PathVariable("deviceid") String deviceid){

        //校验
        if(deviceid==null    ||   "".equals(deviceid)){
            return R.failed(null,"设备id不能为空");
        }
        //1.查询设备表名
        String deviceTableName= smregisterService.getTableName("DevicePoint");
        //2.查询传感器
        List<Map<String, Object>> sensorList=scSensorInfoService.querySensorByDevice(deviceTableName,deviceid);

        return R.ok(sensorList,"查询成功");
    }




    @ApiOperation(value = "绑定（类型和传感器）")
    @PostMapping("/bindTypeAndSensor")
    public R bindTypeAndSensor(@RequestBody List<ScTypeSensor> sensorList){

        //非空校验 devicetype  sensorcode sensorname
        if(sensorList==null  ||  sensorList.size()==0){
            return R.failed(null,"参数不能为空");
        }

        for (ScTypeSensor  sensor: sensorList) {

            if(    StringUtil.isNullOrEmpty(sensor.getSensorCode())
                    ||   StringUtil.isNullOrEmpty(sensor.getSensorName()  )
                    || StringUtil.isNullOrEmpty(sensor.getDeviceType()  )){
                return R.failed(null,"必填项不能为空");
            }
        }

        //1.先解绑
        Map<String,Object>  deleteParam=new HashMap<>();
        deleteParam.put("device_type",sensorList.get(0).getDeviceType());

        boolean  deleteFlag=scTypeSensorService.removeByMap(deleteParam);


        //2.在绑定
        boolean  bindFlag=scTypeSensorService.saveBatch(sensorList);
        if(bindFlag){
            return R.ok(null,"绑定成功");
        }else{
            return R.failed(null,"绑定失败");
        }

    }

    @ApiOperation(value = "解绑（类型和传感器）")
    @GetMapping("/unBindTypeAndSensor/{deviceType}")
    public R unBindTypeAndSensor(@PathVariable("deviceType") String deviceType){

        //非空校验 devicetype  sensorcode sensorname
        if( StringUtil.isNullOrEmpty(deviceType)){
            return R.failed(null,"设备类型不能为空");
        }

        Map<String,Object>  deleteParam=new HashMap<>();
        deleteParam.put("device_type",deviceType);

        boolean  deleteFlag=scTypeSensorService.removeByMap(deleteParam);

        return R.ok(null,"解绑成功");

    }

}
